# -*- coding = utf-8 -*-
# @Time :2024/1/30 21:25
# @Author :invictusQAQ
# @Description :
# @File :  base_page.py

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC


class BasePage:

    def __init__(self, driver):
        self.driver = driver

    # 访问url
    def visit(self, url):
        self.driver.get(url)

    # 元素定位
    def locator(self, loc):
        if WebDriverWait(self.driver, 10).until(EC.presence_of_element_located(loc)):
            return self.driver.find_element(*loc)

    # 输入
    def input(self, loc, txt):
        self.locator(loc).send_keys(txt)

    # 点击
    def click(self, loc):
        self.locator(loc).click()

    # 清除输入框内容
    def clear(self, loc):
        self.locator(loc).clear()

    # 用于处理focus元素：获取当前焦点并输入
    def focus_input(self, txt):
        self.driver.switch_to.active_element.send_keys(txt)

    # 处理confirm弹窗，accept or reject
    def process_confirm(self, boolean):
        if WebDriverWait(self.driver, 10).until(EC.alert_is_present()):
            if boolean:
                self.driver.switch_to.alert.accept()
            else:
                self.driver.switch_to.alert.dismiss()
